package edu.sctu.yueding.adapter;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.List;

import edu.sctu.yueding.R;
import edu.sctu.yueding.activitys.ActivityDetailsActivity;
import edu.sctu.yueding.bean.HomeListViewBean;
import edu.sctu.yueding.bean.Tweets;
import edu.sctu.yueding.bean.VoteBean;
import edu.sctu.yueding.utils.JumpUtils;
import edu.sctu.yueding.utils.httputils.GetVolleyHttp;

/**
 * Created by zhengsenwen on 2016/8/2.
 */
public class IndexListViewAdapter extends BaseAdapter {

    private Activity activity;
    private List<HomeListViewBean> list;

    public IndexListViewAdapter(Activity activity, List<HomeListViewBean> list) {
        this.list = list;
        this.activity = activity;


    }

    /**
     * 刷新数据
     *
     * @param data
     */
    public void fresh(List<HomeListViewBean> data, boolean isAddAll) {
        Log.e("page", data.size() + "size");

        if (isAddAll) {
            this.list.addAll(data);
        } else {
            this.list = data;
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (list != null && list.size() > 0) {
            return list.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(activity).inflate(R.layout.item_index_fragment_listview_news, null);

        ImageView imageView = (ImageView) view.findViewById(R.id.item_index_fragment_listView_news_image);
        TextView title = (TextView) view.findViewById(R.id.item_index_fragment_listView_news_title);
        TextView publishDate = (TextView) view.findViewById(R.id.item_index_fragment_listView_news_publishDate);

        GetVolleyHttp.getInstance(activity).setImageLoader(imageView, R.drawable.photo_pictures_no,
                R.drawable.photo_pictures_no, list.get(position).getThumbnail());
        title.setText(list.get(position).getTitle());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd");
        long time = list.get(position).getCreateDate();
        String dateString = simpleDateFormat.format(time);

        publishDate.setText(dateString);


        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Bundle bundle = new Bundle();

                bundle.putString(ActivityDetailsActivity.ID_TAG, String.valueOf(list.get(position).getId()));

                JumpUtils.jumpActivity(activity, ActivityDetailsActivity.class, bundle);
            }
        });
        return view;
    }
}
